/*
 * arena.h
 *
 *  Created on: Nov 20, 2013
 *      Author: killerg
 */

#ifndef ARENA_H_
#define ARENA_H_

#include "share/util.h"

#define RANK_SIZE 1000
#define OPPO_SIZE 6

struct arena_t {
    bool init();
    bool init_rewarding();
    void save_rewarding();
    bool init_rank();
    void save_rank();
    void destroy();
    void update();

    bool has_player(int64_t playerid) {
        return players.count(playerid) > 0;
    }

    bool is_full() {
        return players.size() >= RANK_SIZE;
    }

    // before一定在榜单中
    // playerid不一定在榜单中
    int insert(int64_t playerid, int64_t before);
    bool is_before(int64_t playerid, int64_t before);
    // 榜单未满，直接加进去
    // return 排名
    int first_insert(int64_t playerid);
    // 查询名词和前面的玩家 return 排名
    int query(int64_t myid, list<int64_t> &lst, int interval=1);
    int get_index(int64_t playerid);

    int get_size() { return players.size(); }
    int64_t get_playerid(int pos) {
        if (pos < 0 || pos >= players.size())
            return -1;

        return array[pos];
    }

    void set_rewarding(bool ok) {
        _rewarding = ok;
        _next_index = (ok?0:-1);
    }

    bool rewarding() { return _rewarding; }
private:
    set<int64_t> players;
    int64_t array[RANK_SIZE];
    bool _inited = false;

    bool _rewarding  = false;
    int _next_index = -1;
    bool _chged = false;
};

#endif /* ARENA_H_ */
